package com.example.demo.config;

import io.swagger.v3.oas.annotations.media.Schema;

public class Result<T> {

    static String successCode = "200";
    static String paramCode = "400";
    static String errorCode = "500";
    @Schema(description = "状态码", defaultValue = "200")
    private String code;
    @Schema(description = "状态信息", defaultValue = "请求成功")
    private String msg;
    private T data;

    public Result() {}

    public Result(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return getResult(successCode, "请求成功", data);
    }

    public static <T> Result<T> error(T data) {
        return getResult(errorCode, "服务器内部错误", data);
    }

    public static <T> Result<T> paramError(T data) {
        return getResult(paramCode, "请求参数错误", data);
    }

    private static <T> Result<T> getResult(String code, String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
